import java.util.HashMap;
import java.util.Random;
import smfl.advertisement;

/**
 * @author keithcasamento
 *
 */
public class Database
{
    private static int investorID=0; //investor id generator
    private static int adID=0; //ad company generator

    /**
     * collection of all Ad Companies
     */
    protected static HashMap<Integer, AdCompany> adcompanies = new HashMap<Integer, AdCompany>();

    /**
     * collection of all Investors
     */
    protected static HashMap<Integer, Investor> investors = new HashMap<Integer, Investor>();

    /**
     * collection of all advertisements
     */
    protected static HashMap<Integer, advertisement> allAds = new HashMap<Integer, advertisement>();

    /**
     * increments the investor id generator
     */
    public void incrementInvestorID()
    {
        investorID++;
    }

    /**
     * increments the ad id count
     */
    public void incrementAdID() {
        //TODO if threading, we need to lock here
        ++adID;
    }

    /**
     * decrements the ad id count
     */
    public void decrementAdID()
    {
        --adID;
    }

    /**
     *
     * @return int investors id
     */
    public int getInvestorID() {
        return investorID;
    }

    /**
     *
     * @return int ad companies id
     */
    public int getAdID()
    {
        return adID;
    }

    /**
     *
     * @param Investor new Investor
     */
    public void addInvestor(Investor i) {
        investors.put(i.getID(), i);
    }

    /**
     *
     * @param AdCompany new advertising company
     */
    public void addAdCompany(AdCompany a) {
        adcompanies.put(a.getadvID(), a);
    }

    /**
     *
     * @param int id of Investor
     * @return Investor
     */
    public Investor getInvestor(int id) {
        return investors.get(id);
    }

    /**
     *
     * @param int id of Advertiser
     * @return AdCompany
     */
    public AdCompany getAdCompany(int id){
        return adcompanies.get(id);
    }

    /**
     *
     * @param int id of investor
     * @return boolean
     */
    public boolean hasInvestor(int id)
    {
            return investors.containsKey(id);
    }

    /**
     *
     * @param int id of ad company
     * @return boolean
     */
    public boolean hasAdCompany(int id)
    {
            return adcompanies.containsKey(id);
    }

    /**
     * Adds a new advertisement and increments the count of open ads.
     * @param ad The ad string to be included
     */
    public void addAd(String text)
    {
        advertisement ad = new advertisement(text, adID);
        allAds.put(adID, ad);
        incrementAdID();
    }

    /**
     * Removes an ad based on its id
     * @param id the id number of the ad to be removed
     */
    public void removeAd(int id)
    {
        allAds.remove(id);
        decrementAdID();
    }

    /**
     * Returns the current list of ads.
     */
     protected advertisement getAd()
     {
         Random generator = new Random();
         boolean valid = false;
         advertisement toReturn = null;

         if (adID == 0)
             return new advertisement("Thank you for investing with Warren's Buffet Stock Market Fantasy League!", -1);

         while (!valid)
         {
             int temp = (generator.nextInt() % adID);
             toReturn = allAds.get(temp);
             if (toReturn != null)
                 valid = true;
         }

         //increment the view count of this ad
         toReturn.incrementViews();
         return toReturn;
     }
     
    /**
     *
     * @param Investor updates Investor i
     */
    public void updateInvestor(Investor i)
    {
            investors.remove(i.getID());
            investors.put(i.getID(), i);
    }

    /**
     *
     * @param AdCompany updates AdCompany a
     */
    public void updateAdCompany(AdCompany a)
    {
            adcompanies.remove(a.getadvID());
            adcompanies.put(a.getadvID(), a);
    }
    
    /**
     * 
     * @param id Ad company id to remove
     */
    public void removeAdCompany(int id)
    {
        adcompanies.remove(id);
    }
}